﻿using Sails.FileMng.Ini;
using System;
using System.Diagnostics;
using System.IO;

namespace Sails.Log
{

    public enum LogLevel
    {
        /// <summary>
        /// Debug
        /// </summary>
        Log_Debug = 0,
        /// <summary>
        /// 普通日志
        /// </summary>
        Log_Info = 1,
        /// <summary>
        /// 警告信息
        /// </summary>
        Log_Waring = 2,
        /// <summary>
        /// 错误日志
        /// </summary>
        Log_Error = 3
    }

    /// <summary>
    /// 日志管理器
    /// </summary>
    public class LogManager:IDisposable
    {
        /// <summary>
        /// 是否记录日志到文件
        /// </summary>
        public static bool IsWriteFile = true;
        /// <summary>
        /// 是否进行网络公布
        /// </summary>
        public static bool IsNetSend = false;
        /// <summary>
        /// 日志存放路径
        /// </summary>
        public static string LogPath = AppDomain.CurrentDomain.BaseDirectory + "Log/";
        /// <summary>
        /// 文件记录的最大大小K
        /// </summary>
        public static int LogFileMaxSize = 100;
        /// <summary>
        /// 日志网络监听端口
        /// </summary>
        public static int LogNetListenerPort = 50000;
        /// <summary>
        /// 日志记录级别
        /// </summary>
        public static int LogWriteLevel = 0;
        

        #region 全局单例
        /// <summary>
        /// 单例静态对象
        /// </summary>
        private static LogManager logManager = null;
        /// <summary>
        /// 获取单例静态对象
        /// </summary>
        /// <returns></returns>
        public static LogManager GetLM()
        {
            if(logManager==null)
            {
                logManager = new LogManager();
            }
            return logManager;
        }
        #endregion

        #region 属性
        
        /// <summary>
        /// 写入日志的大小
        /// </summary>
        private int WriteLogToFileSize = 0;
        /// <summary>
        /// 当前的日志文件名称
        /// </summary>
        private string nowLogFileName = "";
        #endregion

        private LogManager()
        {
            #region 加载配置
            INIFile iniFile = new INIFile(AppDomain.CurrentDomain.BaseDirectory + "config/config.ini");
            string siswritefile = iniFile.IniReadValue("log", "iswritefile");
            string sisnetsend = iniFile.IniReadValue("log", "isnetsend");
            string slogfilemaxsize = iniFile.IniReadValue("log", "logfilemaxsize");
            string snetlistenerport = iniFile.IniReadValue("log", "lognetlistenerport");
            string sloglevel = iniFile.IniReadValue("log", "loglevel");
            if(!string.IsNullOrEmpty(siswritefile))
            {
                int iiswritefile = 0;
                if(int.TryParse(siswritefile,out iiswritefile))
                {
                    IsWriteFile = iiswritefile > 0;
                }
            }
            if (!string.IsNullOrEmpty(sisnetsend))
            {
                int iiswritefile = 0;
                if (int.TryParse(sisnetsend, out iiswritefile))
                {
                    IsNetSend = iiswritefile > 0;
                }
            }
            if (!string.IsNullOrEmpty(slogfilemaxsize))
            {
                int iiswritefile = 0;
                if (int.TryParse(slogfilemaxsize, out iiswritefile))
                {
                    LogFileMaxSize = iiswritefile;
                }
            }
            if(!string.IsNullOrEmpty(snetlistenerport))
            {
                int iiswritefile = 0;
                if (int.TryParse(snetlistenerport, out iiswritefile))
                {
                    LogNetListenerPort = iiswritefile;
                }
            }
            if (!string.IsNullOrEmpty(sloglevel))
            {
                int iiswritefile = 0;
                if (int.TryParse(sloglevel, out iiswritefile))
                {
                    LogWriteLevel = iiswritefile;
                }
            }
            #endregion
            
        }


        #region 写日志
        /// <summary>
        /// 错误日志
        /// </summary>
        /// <param name="info"></param>
        public static void Error(string info)
        {
            Log(LogLevel.Log_Error, info);
        }
        /// <summary>
        /// 输出警告信息
        /// </summary>
        /// <param name="info"></param>
        public static void Waring(string info)
        {
            Log(LogLevel.Log_Waring, info);
        }

        /// <summary>
        /// 输出信息
        /// </summary>
        /// <param name="info"></param>
        public static void Info(string info)
        {
            Log(LogLevel.Log_Info, info);
        }
        /// <summary>
        ///  输出调试信息
        /// </summary>
        /// <param name="info"></param>
        public static void Debug(string info)
        {
            Log(LogLevel.Log_Debug, info);
        }
        /// <summary>
        /// 静态函数。调用日志写入
        /// </summary>
        /// <param name="level"></param>
        /// <param name="info"></param>
        public static void Log(LogLevel level,string info)
        {
            if ((int)level >= LogWriteLevel
#if DEBUG
                || 0==0
#endif
                )
            {
                string temp = string.Format("{0}\n{1}\t{2}", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")
                    , level == LogLevel.Log_Debug ? "Debug" : (level == LogLevel.Log_Info ? "Info" : (level == LogLevel.Log_Waring ? "Waring" : "Error"))
                    , info);
//#if DEBUG
                Trace.WriteLine(temp);
                Console.WriteLine(temp);
//#endif
                if(IsWriteFile)
                {
                    LogManager.GetLM().WriteLogToFile(temp);
                }
                if(IsNetSend)
                {
                    LogManager.GetLM().WriteLogToNet(temp);
                }
            }
        }
        #endregion

        #region 将日志写入文件
        /// <summary>
        /// 将日志内容写入文件
        /// </summary>
        /// <param name="info"></param>
        private void WriteLogToFile(string info)
        {
            if (nowLogFileName=="")
            {
                nowLogFileName = DateTime.Now.ToString("yyyyMMddhhmmss") + ".log";
            }
            try
            {
                if(!Directory.Exists(LogPath))
                {
                    Directory.CreateDirectory(LogPath);
                }
                using (StreamWriter sw = new StreamWriter(LogPath + nowLogFileName, true))
                {
                    sw.WriteLine(info);
                    WriteLogToFileSize += info.Length;
                    sw.Close();
                }
            }
            catch(Exception ex)
            {
                Trace.WriteLine(ex.ToString());
            }
            if(WriteLogToFileSize>LogFileMaxSize*1024)
            {
                nowLogFileName = DateTime.Now.ToString("yyyyMMddhhmmss") + ".log";
            }
        }
        #endregion

        #region 将日志公布到网络
        /// <summary>
        /// 将日志发布到网络
        /// </summary>
        /// <param name="info"></param>
        private void WriteLogToNet(string info)
        {
        }
        #endregion


        public void Dispose()
        {
           
        }
    }
}
